Select = function(options){
	var _options = {};
	$.extend(_options,this._options,options);
	$.extend(this,{
		_options:_options
	});
	
	this._init();
};
Select.prototype = {
	_options:{
		element:null,
		value:null,
		url:null,
		field:"parentId",
		select:null,
		data:{}
	},
	_chain:null,
	_value:null,
	_select:null,
	_init:function(){
		this._chain = [];
		this._value = this._options.value;
		this._select = this._options.select;
		this.select();
		this.registerEvent();
	},
	registerEvent:function(){
		$(this._options.element).change($.proxy(this.onSelect,this));
	},
	select:function(value){
		if(value){
			this._value = value;
		}
		$(this._options.element).val(this._value);
	},
	load:function(chainFieldValue){
		if(chainFieldValue){
			var string = "\{\""+this._options.field+"\":\""+chainFieldValue+"\"\}";
			var params = JSON.parse(string);
			$.extend(this._options.data,params);
		}
		this.empty();
		var option = {
			url:this._options.url,
			data:this._options.data,
			success:$.proxy(function(data){
				if(data){
					for(var idx in data){
						option = document.createElement("option");
						option.setAttribute("value", data[idx]);
						option.appendChild(document.createTextNode(idx));
						
						$(this._options.element).append(option);
					}
					if(this._value){
						$(this._options.element).val(this._value);
						this.onSelect();
					}
				}
			},this)
		};
		new Ajax(option);
	},
	empty:function(){
		$(this._options.element).empty();

		var option = null;
		option = document.createElement("option");
		option.setAttribute("value", "");
		option.appendChild(document.createTextNode("请选择"));
		
		$(this._options.element).append(option);
	},
	onSelect:function(){
		this._value=$(this._options.element).val();
		if(!this._value){
			$(this._options.element).val("");
		}
		
		if(this._chain){
			if(this._value){
				for(var item in this._chain){
					this._chain[item].load(this._value);
				}
			}else{
				for(var item in this._chain){
					this._chain[item].empty();
				}
			}
		}
		if(this._select){
			this._select(this._value);
		}
	},
	chain:function(select){
		this._chain.push(select);
	}
};